#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import threading
import time
import traceback

import pyautogui
from selenium.webdriver.common.by import By
from time import sleep
from base.ObjectMap import ObjectMap
from base.PrintBatchBusCardLoginBase import PrintBatchBusCardLoginBase
from logs.log import log


class PrintBatchBusCardLoginPage(PrintBatchBusCardLoginBase, ObjectMap):
    def login_input_value(self, pwd):
        sleep(2)
        print("输入密码")
        length = len(pwd)
        for i in range(length):
            print(pwd[i])
            pyautogui.press(pwd[i])
        # 敲击回车键
        print("回车")
        pyautogui.press('enter')

    def login_click(self, driver):
        log.info("点击登录")
        button_xpath = self.login_button()
        return self.element_click(driver, By.XPATH, button_xpath)

    def to_login(self, driver, url, pwd):
        log.info("跳转登录页面")
        self.element_to_url(driver, url)
        self.mouse_thread = threading.Thread(target=self.login_input_value, kwargs={"pwd": pwd})
        self.mouse_thread.start()
        self.login_click(driver)
        sleep(1)
        # self.login_input_value(pwd)
        # sleep(1)

    def to_print(self, driver, data, pwd):
        # @TODO 选择登录用户
        # self.element_click(driver, By.XPATH, "")
        # 预算执行
        # self.element_click(driver, By.XPATH, '//*[@id="pay"]')

        url = driver.current_url
        query = url.split('?')[1]
        new_url = f"https://59.255.67.8/pay/index.html?{query}&appguid=pay#/HomeCard"
        print(f"打开新页面：${new_url}")
        self.element_to_url(driver, new_url)

        time.sleep(0.5)

        # 菜单
        self.element_click(driver, By.XPATH, '//span[text()="菜单"]')
        # 预算执行
        self.element_click(driver, By.XPATH, '//span[contains(text(),"预算执行")]/../../i')
        # 部门支出预算执行
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"部门支出预算执行")]/../../i')
        # 公务卡管理
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"公务卡管理")]/../../i')
        # # 公务卡消费明细管理（预算单位）
        self.element_click(driver, By.XPATH,
                           '//span[contains(text(),"公务卡消费明细管理（预算单位）")]')
        time.sleep(25)
        count = 0
        for person in data:
            person['images'] = []
            try:
                # 请输入持卡人姓名
                self.element_fill_value1(driver, By.XPATH,
                                         '//input[@placeholder="请输入持卡人姓名（查询银行推送数据）"]',
                                         person['姓名'], 30)

                # 请输入卡号后四位
                self.element_fill_value1(driver, By.XPATH,
                                         '//input[@placeholder="请输入卡号后四位（查询银行推送数据）"]',
                                         person['银行卡号'][-4:], 30)
                # 请输入最小时间
                self.element_fill_value1(driver, By.XPATH,
                                         "//input[@placeholder='请选择起始刷卡日期']", person['最小时间'], 30)
                # 请输入最大时间
                self.element_fill_value1(driver, By.XPATH,
                                         "//input[@placeholder='请选择结束刷卡日期']", person['最大时间'], 30)
                # 请输入最小报销金额
                self.element_fill_value1(driver, By.XPATH,
                                         '//input[@placeholder="请输入最小报销金额"]',
                                         person['实际消费金额'], 30)
                # 请输入最大报销金额
                self.element_fill_value1(driver, By.XPATH,
                                         '//input[@placeholder="请输入最大报销金额"]',
                                         person['实际消费金额'], 30)
                time.sleep(2)

                # 点击搜索
                self.element_click(driver, By.XPATH,
                                   '//button/span[text()="搜索"]')
                time.sleep(5)
                rows = len(self.find_element_rows(driver, By.XPATH,
                                                  '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[2]/table/tbody/tr'))
                print(rows)
                # self.assertEqual(person['姓名']+person['最小报销金额'],rows,1);#验证是否为1
                if rows < 1:
                    person['images'].append(self.save_screenshot1(driver))
                    log.info(f"数据：{json.dumps(person)}，找到数据：{rows}，该执行取消")
                    person['success'] = False
                    person['message'] = f"找到数据：{rows}，该执行取消"
                else:
                    if rows > 1:
                        self.element_click(driver, By.XPATH,
                                           '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[2]/table/tbody/tr[1]/td[1]/div/span/span[2]')
                    # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[2]/table/tbody/tr[1]/td[1]/div/span
                    else:
                    # # 全选
                        self.element_click(driver, By.XPATH,
                                           '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[1]/table/thead/tr/th[1]/div[1]/span/span')
                        # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[1]/table/thead/tr/th[1]/div[1]/span/span/span[2] 全选

                    time.sleep(2)
                    # # 点击确认报销
                    self.element_click(driver, By.XPATH,
                                       '//button[@title="确认报销"]')
                    if person['实际消费金额'] != person['实际报销金额']:
                        # 点击加号
                        self.element_click(driver, By.XPATH,
                                           '//*[@id="app"]/div/div[3]/div[2]/div/div[5]/div/div[2]/div/div[2]/i')
                        time.sleep(0.5)
                        self.element_fill_value1(driver, By.XPATH,
                                                 '//*[@id="app"]/div/div[3]/div[2]/div/div[5]/div/div[2]/div/div[3]/div/div/input',
                                                 person['实际报销金额'], 30)
                        time.sleep(1)
                    # 点击确定
                    self.element_click(driver, By.XPATH,
                                       '//*[@id="app"]/div/div[3]/div[2]/div/div[5]/div/div[3]/div/button[2]')
                    # //button/span[contains(text(),"确定")]
                    time.sleep(2.5)
                    # 点击生成支付申请
                    self.element_click(driver, By.XPATH,
                                       '//button/span[text()="生成支付申请"]')
                    self.element_click(driver, By.XPATH,
                                       '//span[text()="公务卡消费明细管理（预算单位）"]')
                    person['success'] = True
                    person['message'] = ""
                    count = count + 1
                    person['images'].append(self.save_screenshot1(driver))
            except BaseException as e:
                log.error(traceback.format_exc())
                person['success'] = False
                person['message'] = str(e)
                person['images'].append(self.save_screenshot1(driver))
                pass
            # else:
            #     log.error(traceback.format_exc())
            #     person['success'] = False
            #     person['message'] = traceback.format_exc()
            #     pass
        sum = len(data)
        if sum == count:
            time.sleep(1.5)
            # 跳转到集中支付页面
            self.element_click(driver, By.XPATH,
                               '//span[text()="公务卡支付申请生成（集中支付）"]')
            time.sleep(2)
            # 点击每页条数
            self.element_click(driver, By.XPATH,
                               '//*[@id="app"]/div/div[3]/div[2]/div/div[1]/div/div[2]/div/div[3]/main/div/div[1]/div[3]/div/div/div/div[2]/span/i')
            # 选择 10000条
            self.element_click(driver, By.XPATH,
                               '//div[text()="10000条"]')
            time.sleep(10)
            # 全选
            self.element_click(driver, By.XPATH,
                               '//*[@id="app"]/div/div[3]/div[2]/div/div[1]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[1]/table/thead/tr/th[1]/div[1]/span/span')
            # //*[@id="app"]/div/div[3]/div[2]/div/div[1]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[3]/div[1]/table/thead/tr/th[1]/div[1]/span/span
            # /html/body/div[1]/div/div[3]/div[2]/div/div[1]/div/div[2]/div/div[3]/main/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[1]/div[1]/span/span

            # //span[@title="全选/取消"]
            # # 生成支付申请
            self.element_click(driver, By.XPATH,
                               '//button[@title="生成支付申请"]')

            time.sleep(13)

            # 关键字过滤
            self.element_fill_value1(driver, By.XPATH,
                                     '//input[@placeholder="关键字过滤"]',
                                     person['政府支出功能分类'], 30)
            time.sleep(3)
            for i in range(2):
                self.right_click(driver, By.XPATH,
                                 '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[3]/div[1]')
                # 隐藏列
                self.element_click(driver, By.XPATH,
                                   "//span[text()='隐藏列']")
                # 隐藏当前列
                self.element_click(driver, By.XPATH,
                                   "//span[text()='隐藏当前列']")
            for i in range(6):
                self.right_click(driver, By.XPATH,
                                 '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[4]/div[1]')
                # 隐藏列
                self.element_click(driver, By.XPATH,
                                   "//span[text()='隐藏列']")
                # 隐藏当前列
                self.element_click(driver, By.XPATH,
                                   "//span[text()='隐藏当前列']")
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[6]/div[1]
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[7]/div[1]
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[8]/div[1]
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[9]/div[1]
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[10]/div[1]
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[2]/div[1]/table/thead/tr/th[11]/div[1]
            # 项目名称
            self.element_click(driver, By.XPATH,
                               '//span[text()="项目名称"]/../span/i[@title="对所选的列启用筛选"]')
            # # 输入内容
            self.element_fill_value1(driver, By.XPATH,
                                     "//input[@placeholder='输入关键字过滤']", person['项目名称'], 30)
            time.sleep(1)
            self.element_click(driver, By.XPATH,
                               "//span[text()='筛选']")
            # 项目名称
            self.element_click(driver, By.XPATH,
                               '//span[text()="支出功能分类"]/../span/i[@title="对所选的列启用筛选"]')
            # # 输入内容
            self.element_fill_value1(driver, By.XPATH,
                                     "//input[@placeholder='输入关键字过滤']", person['支出功能分类'], 30)
            time.sleep(1)
            self.element_click(driver, By.XPATH,
                               "//span[text()='筛选']")

            # 指标类型
            self.element_click(driver, By.XPATH,
                               '//span[text()="指标类型"]/../span/i[@title="对所选的列启用筛选"]')
            # # 输入内容
            self.element_fill_value1(driver, By.XPATH,
                                     "//input[@placeholder='输入关键字过滤']", person['指标类型'], 30)
            time.sleep(1)
            self.element_click(driver, By.XPATH,
                               "//span[text()='筛选']")
            # 选择项目单选框
            self.element_click(driver, By.XPATH,
                               '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[3]/div[2]/table/tbody/tr[1]/td/div/span')
            time.sleep(2)

            # 先定位链接的位置
            link_position = (900, 850)
            # 然后模拟鼠标移动到链接位置
            pyautogui.moveTo(link_position, duration=1)
            # 模拟单次点击
            pyautogui.click(clicks=1, interval=1)
            # # 等待页面加载完成
            time.sleep(2)
            print("开始滚动")
            for i in range(4):
                self.roll_click(driver, By.XPATH,
                                '//*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]')
            print("结束滚动")
            time.sleep(4)
            self.element_click(driver, By.XPATH,
                               '//input[@placeholder="部门经济分类"]/../span/span/i')
            # //*[@id="app"]/div/div[3]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div[5]/div[3]/div[2]/form/div[1]/div[4]/div/div[2]/div/div/div/div/div/div/div/div/div/div/span/span/i
            # //input[text()="支出功能分类"]/../span/i[@title="对所选的列启用筛选"]
            time.sleep(2)
            self.element_fill_value1(driver, By.XPATH,
                                     '//input[@placeholder="输入关键字进行过滤"]', person['部门经济分类'], 30)
            time.sleep(2)
            # "//span[text()="+person['部门经济分类']+"]"
            self.double_click(driver, By.XPATH, '//span[text()=' + '"' + person['部门经济分类'] + '"' + ']')
            time.sleep(2)

            # 资金用途
            self.element_fill_value1(driver, By.XPATH,
                                     '//input[@placeholder="请输入资金用途"]',
                                     person['资金用途'], 30)
            time.sleep(1)
            # 保存并送审（按钮）
            self.element_click(driver, By.XPATH,
                               '//button/span[text()="保存并送审"]')
            time.sleep(15)
            # 先定位链接的位置
            link_position = (900, 850)
            # 然后模拟鼠标移动到链接位置
            pyautogui.moveTo(link_position, duration=1)
            # 模拟单次点击
            pyautogui.click(clicks=1, interval=1)
            # # 等待页面加载完成
            time.sleep(5)
            length = len(pwd)
            for i in range(length):
                print(pwd[i])
                pyautogui.press(pwd[i])
            # 敲击回车键
            print("回车")
            pyautogui.press('enter')
            time.sleep(2)
        return data
